package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyVetoException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.ExecutionException;

import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingWorker;
import javax.swing.ViewportLayout;



import framework.classification.entities.ClAbstractEntity;
import framework.classification.library.AbstractClassfication;
import framework.entity.ACLComponent;
import framework.entity.ACLFunction;
import framework.entity.ACLInterface;
import gui.controller.Controller;
import gui.search.AdvencedSearch;
import gui.utils.RoundedBorder;
import gui.viewEntity.ViewACLComponent;
import gui.viewEntity.ViewACLEntity;
import gui.viewEntity.ViewACLFunction;
import gui.viewEntity.ViewACLInterface;
import gui.viewLattice.ViewPDF;
import gui.viewLattice.ViewLattice;

public class ViewDirectory extends JPanel{


	public static String CASCADE = "cascade";
	public static String ALL_REDUCED = "allreduced";
	public static String ALIGN = "align";


	private AbstractClassfication classification;
	private AdvencedSearch search;
	private Controller controller;
	private JDesktopPane desktop;

	public ViewDirectory (AbstractClassfication classfication){

		super();
		this.classification = classfication;
		this.setLayout(new BorderLayout());
		this.add(createToolBar(),BorderLayout.NORTH);
		this.add(createComponent(),BorderLayout.CENTER);
		this.setBackground(Color.WHITE);

	}

	public ViewDirectory (AbstractClassfication classfication,Controller controller){

		super();
		this.controller = controller;
		this.classification = classfication;
		this.setLayout(new BorderLayout());
		this.add(createToolBar(),BorderLayout.NORTH);
		this.add(createComponent(),BorderLayout.CENTER);
		this.setBackground(Color.WHITE);


	}
	private JComponent createToolBar(){

		Box mainBox = Box.createHorizontalBox();
		search = new AdvencedSearch(classification,controller);
		//search.setMinimumSize(new Dimension(750,60));
		search.setMaximumSize(new Dimension(1250,40));
		search.setPreferredSize(new Dimension(1250,40));
		search.setBackground(Color.WHITE);

		mainBox.add(Box.createHorizontalGlue());
		mainBox.add(search);
		mainBox.add(Box.createHorizontalGlue());
		return mainBox;

	}

	private JComponent createComponent(){
		desktop = new JDesktopPane();
		desktop.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				search.getAutoCompletion().closeDialog();
			}
		});
		return desktop;
	}



	public void addFrame(final String name){
		SwingWorker	worker = new SwingWorker() {
			protected JInternalFrame doInBackground() throws Exception {
				
				if (classification.getComponentsNames().containsKey(name)){

					ACLComponent ac = classification.getComponentsNames().get(name);
					
					ViewACLComponent vv = new ViewACLComponent(ac,classification,controller);
					return createInternalFrame(ac.getName(),vv);

				}
				if (classification.getInterfacesNames().containsKey(name)){
					ACLInterface ac = classification.getInterfacesNames().get(name);
					ViewACLInterface vv = new ViewACLInterface(ac,classification,controller);
					return createInternalFrame(ac.getName(),vv);
				}
				if (classification.getFunctionRequired().containsKey(name)){
					ACLFunction ac = new ACLFunction(name,ACLFunction.MODE2);
					ViewACLFunction vv = new ViewACLFunction(ac,classification,controller);
				return createInternalFrame(ac.getName(),vv);
				}
				else if(classification.getFunctionsProvided().containsKey(name)){
					ACLFunction ac = new ACLFunction(name,ACLFunction.MODE2);
					ViewACLFunction vv = new ViewACLFunction(ac,classification,controller);
					return createInternalFrame(ac.getName(),vv);
					
				}
				return null;
			}
			
			protected void done(){
				
				try {
					desktop.add((JInternalFrame) this.get());
					System.out.println("couou");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		};
		worker.execute();

	}

	public void addFrameLattice(ClAbstractEntity cl){
		JInternalFrame f = new JInternalFrame("lattice");

		ViewLattice vl = new ViewLattice(cl);
		f.setContentPane(vl);
		f.setSize(400,500);
		f.setVisible(true);
		f.setMaximizable(true);
		f.setClosable(true);f.setIconifiable(true);
		f.setResizable(true);
		desktop.add(f);

	}

	public JInternalFrame createInternalFrame(String name,ViewACLEntity vv){
		JInternalFrame f = new JInternalFrame(name);
		f.setContentPane(vv);
		f.setSize(550,450);
		f.setVisible(true);
		f.setMaximizable(true);
		f.setClosable(true);f.setIconifiable(true);
		f.setResizable(true);
		return f;
	}

	public void positionInternalFrame(String mode){
		if (mode.equals(ALIGN)){
			int decalageX = 0;
			int decalageY = 0;
			for (JInternalFrame nal : desktop.getAllFrames()){
				try {
					nal.setIcon(false);
					nal.setSize(550,450);
					nal.setLocation(decalageX,decalageY);

					decalageX = decalageX + 550;
					System.out.println(decalageY+" "+getHeight());
					if ((decalageX +250)>= desktop.getWidth()){

						decalageY = 450;
						decalageX = 0;
					}

				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		else if (mode.equals(ALL_REDUCED)){
			for (JInternalFrame nal : desktop.getAllFrames()){

				try {
					nal.setIcon(true);
				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else if(mode.equals(CASCADE)){
			int decalageX = 0;
			int decalageY = 0;
			for (JInternalFrame nal : desktop.getAllFrames()){
				try {
					nal.setIcon(false);
					nal.setSize(550,440);
					nal.setLocation(decalageX,decalageY);

					decalageX = decalageX + 50;
					decalageY = decalageY + 50;
					System.out.println(decalageY+" "+getHeight());
					if ((decalageY +300)>= desktop.getHeight()){

						decalageY = 0;
						decalageX = 200;
					}

				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}


		}

	}

	public AbstractClassfication getAbstractClassification(){return classification;}
	public void setController(Controller c){controller = c;}
	public AdvencedSearch getSearch(){return search;}
}
